package com.codejiwei.core.demo

import org.apache.spark.sql.{DataFrame, SparkSession}

object MySqlDataSync {
  def main(args: Array[String]): Unit = {

    val ss = SparkSession.builder()
      .appName("LoadMySQLData")
      .master("local[2]")
      .getOrCreate()

    val jdbcDF: DataFrame = ss.read.format("jdbc")
      .option("url", "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&useSSL=false")
      .option("dbtable", "flink_score")
      .option("user", "root")
      .option("password", "123456")
      .load()

//    jdbcDF.printSchema()
//    jdbcDF.show()


    jdbcDF.write.mode("append")
      .format("jdbc")
      .option("url", "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&useSSL=false")
      .option("dbtable", "flink_score_bak")
      .option("user", "root")
      .option("password", "123456")
      .save()

    ss.close()
  }

}
